<!--
 * @Descripttion: 
 * @Version: 
 * @Author: gaohj
 * @Date: 2022-10-29 11:06:21
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-10-29 12:07:09
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 普通字符串 
        // '!@#$%^&ASDFGZXCVBNwersd1f'
        //数字字符串
        // '12345678'
        // html 格式字符串 
        // '<h1>asdfasdf</h1>'
        // 查询字符串  

        //https://www.baidu.com/s?wd=%E6%96%B0%E4%B9%A1%E4%B8%96%E7%95%8C%E6%9D%AF&rsv_spt=1&rsv_iqid=0x9aab659e000468bc&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_dl=ib&rsv_sug3=27&rsv_sug1=17&rsv_sug7=100
        // https://  http://  传输协议  
        //www.baidu.com 域名  => 19.47.85.192
        // s   路径   search    等同于 http://www.qfedu.com/html5/   html5 
        // ? 后边是查询字符串 
        // wd=%E6%96%B0%E4%B9%A1%E4%B8%96%E7%95%8C%E6%9D%AF&rsv_spt=1&rsv_iqid=0x9aab659e000468bc&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_dl=ib&rsv_sug3=27&rsv_sug1=17&rsv_sug7=100
        // wd=cba
        // rsv_spt=1
        // ie=utf-8

        var person_info = 'username=kangbazi&age=18&height=181cm&weight=100kg&email=tet@qq.com';


        // => {username:'kangbazi',age:18,'email':'test@qq.com'}
        // => split('&') => username=kangbazi
        // => split('=')
        /**
         * @function: parseQueryString 
         * @description: 解析查询字符串 得到对象
         * @param {String} str  'username=kangbazi&age=18&height=181cm&weight=100kg&email=tet@qq.com'
         * @return {Array} {username:'kangbazi',age:18,'email':'test@qq.com'}
         * @author: gaohj
         * @example: parseQueryString('username=kangbazi&age=18')
         */

        function parseQueryString(str) {
            var maps = {};
            var res1 = str.split('&');
            res1.forEach(function (item) {
                // item username=kangbazi
                var res2 = item.split('=');
                var key = res2[0];
                var value = res2[1];
                maps[key] = value;
            });
            return maps;
        }


        var test = parseQueryString(person_info);

        console.log(test);


        var obj1 = {
            'username': 'zhangsan',
            'age': 18,
            'email': 'test@qq.com'
        };
        // username=zhangsan&age=18&email=test@qq.com

        var strs = '';
        for (var k in obj1) {
            // console.log(k,obj[k])
            strs += `${k}=${obj1[k]}&`;
        }
        console.log(strs); //username=zhangsan&age=18&email=test@qq.com&
        strs = strs.slice(0, -1); // 把最后的& 删除
        console.log(strs); //username=zhangsan&age=18&email=test@qq.com

        /**
         * @function: 
         * @description: 
         * @param {*} obj
         * @return {*}
         * @author: gaohj
         * @example: 
         */        
        function object_to_querystring(obj) {
            var strs = '';
            for (var k in obj) {
                strs += `${k}=${obj[k]}&`;
            }
            strs = strs.slice(0, -1); // 把最后的& 删除
            return strs;

        }


    </script>
</body>

</html>